let nums = [1, 3]
let target = 2

function binarySearch(nums, target) {
  let left = 0; // 左边界
  let right = nums.length - 1; // 右边界

  if(target < nums[left]){
    return 0
  }
  if(target > nums[right]){
    return right + 1
  }
  let mid = 0
  while (left <= right) {
    mid = Math.floor((left + right) / 2);
    if (nums[mid] == target) {
      return mid;
    } else if (nums[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return left;
}

let index = binarySearch(nums, target)
console.log(index)